//分离成mode
// const users = new Map([["tom","123456"],["jack","654321"]])
//map 可以接受数组作为参数，数组成员还是一个数组，其中有俩元素，一个表示key一个表示value

const userDao = require('../dao/userDao') //根据mvc架构，将userDao查操作结果交给service
const encryption = require('../lib/encryption');

const Sequelize = require("sequelize");
const Op = Sequelize.Op;

module.exports = {
    checkLogin:async(phone,password)=>{
        // let user = await userDao.findUserByPhone(phone);
        //调用userDao中的sequelize的方法
        let user = await userDao.getUserByPhone(phone);
        if(user){
            // if(user.password == password){ //判断user的password是否与输入的密码一致
            //     return user;
            // }

            //对密码进行加密处理，把password和salt绑定在一起
            let encryPass = await encryption.getMd5Pass(password,user.salt);
            if(encryPass == user.password){
                return user;
            }
        }
        return null;
    },
    seachUserByInfo:async(serchInfo)=>{
        //主要用于处理查询条件
        const likeQuery = ['username','phone'];
        for(let key in serchInfo){
            if (serchInfo[key] === '' || serchInfo[key] === undefined) delete serchInfo[key];
            else {
                if (likeQuery.includes(key)) {
                    serchInfo[key] = {
                        [Op.like]: `%${serchInfo[key]}%`,
                    };
                }
            }
        }
        //查询条件交给dao执行查询
        console.log(serchInfo);
        console.log("111111111");
        let searchResult = await userDao.getUsersBySearchInfo(serchInfo);
        return searchResult;
    },
    saveUser:async(userInfo)=>{
        if(userInfo.phone){
            let user = await userDao.getUserByPhone(userInfo.phone);
            if(!user){
                let salt = encryption.generateId();
                console.log(salt);
                let encryPass = await encryption.getMd5Pass("123456",salt);
                userInfo.salt = salt;
                userInfo.password = encryPass;
                let result = await userDao.createUser(userInfo);
                if(result){
                    return {success:1,info:'新增用户成功'}
                }else{
                    return {success:0,info:'新增用户失败！'}
                }
            }else{
                return {success:0,info:'该手机号码已存在！'}
            }
        }
        return {success:0,info:'信息提交有误！'};
    },
    updateUser:async(userInfo)=>{
        if(userInfo.id){
            let user = await userDao.findUserByPk(userInfo.id);
            let phoneUsers = await userDao.getAllUsersByPhone(user.phone);
            if(phoneUsers.length == 1){
                let result = await userDao.updateUser(userInfo.id,{
                    username:userInfo.username,
                    phone:userInfo.phone,
                    roleid:userInfo.roleid
                })
                if(result){
                    return {updateuser:userInfo,success:1,info:'修改用户成功'}
                }else{
                    return {updateuser:userInfo,success:0,info:'修改用户失败！'}
                }
            }else{
                return {updateuser:userInfo,success:0,info:'该手机号码已存在！'}
            }
        }return {updateuser:userInfo,success:0,info:'信息提交有误！'};
    },
}